package com.tancire.arts.controller;

import com.tancire.arts.domain.MoveArt;
import com.tancire.arts.domain.NoteArticle;
import com.tancire.arts.domain.NoteComposition;
import com.tancire.arts.service.INoteArticleService;
import com.tancire.arts.service.INoteCompositionService;
import com.tancire.common.annotation.Client;
import com.tancire.common.annotation.Log;
import com.tancire.common.core.controller.BaseController;
import com.tancire.common.core.domain.AjaxResult;
import com.tancire.common.core.page.TableDataInfo;
import com.tancire.common.enums.BusinessType;
import com.tancire.common.enums.UserSource;
import com.tancire.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenchao
 * @version 1.0
 * @since 2024/9/23 14:17
 */
@RestController
@RequestMapping("/arts/note")
public class NoteMemberController extends BaseController {

    @Resource
    private INoteCompositionService noteCompositionService;

    @Resource
    private INoteArticleService noteArticleService;

    /**
     * 查询作品集列表
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping("/composition/list")
    public TableDataInfo listComposition(NoteComposition noteComposition) {
        noteComposition.setCreateBy(SecurityUtils.getUserId().toString());
        startPage();
        List<NoteComposition> list = noteCompositionService.selectNoteCompositionList(noteComposition);
        return getDataTable(list);
    }

    /**
     * 新增作品集
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @PostMapping("/composition/add")
    public AjaxResult addComposition(@RequestBody NoteComposition noteComposition) {
        return toAjax(noteCompositionService.insertNoteComposition(noteComposition));
    }

    /**
     * 修改作品集
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @PutMapping("/composition/edit")
    public AjaxResult editComposition(@RequestBody NoteComposition noteComposition) {
        return toAjax(noteCompositionService.updateNoteComposition(noteComposition));
    }

    /**
     * 获取作品集详细信息
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping(value = "/composition/info/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        NoteComposition noteComposition = noteCompositionService.selectNoteCompositionById(id);
        if (noteComposition.getCreateBy().equals(SecurityUtils.getUserId().toString())) {
            return AjaxResult.success(noteComposition);
        }
        return AjaxResult.error("查询详情信息失败");
    }

    /**
     * 查询文章管理列表
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping("/art/tree")
    public AjaxResult list(NoteArticle noteArticle) {
        List<NoteArticle> list = noteArticleService.selectNoteArticleTree(noteArticle);
        return AjaxResult.success(list);
    }

    /**
     * 获取文章管理详细信息
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @GetMapping(value = "/art/info/{id}")
    public AjaxResult getArtInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(noteArticleService.selectNoteArticleById(id));
    }

    /**
     * 新增文章管理
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @PostMapping("/art/add")
    public AjaxResult addArt(@RequestBody NoteArticle noteArticle) {
        return toAjax(noteArticleService.insertNoteArticle(noteArticle));
    }

    /**
     * 修改文章管理
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @PutMapping("/art/edit")
    public AjaxResult editArt(@RequestBody NoteArticle noteArticle) {
        return toAjax(noteArticleService.updateNoteArticle(noteArticle));
    }

    /**
     * 删除文章管理
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @DeleteMapping("/art/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(noteArticleService.deleteNoteArticleByIds(ids));
    }

    /**
     * 新增文章管理
     */
    @Client(UserSource.MEMBER)
    @PreAuthorize("@ss.hasPermi('member:user:union')")
    @PostMapping("/art/move")
    public AjaxResult moveArt(@RequestBody MoveArt moveArt) {
        noteArticleService.moveNoteArticle(moveArt);
        return AjaxResult.success("移动成功");
    }
}
